﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Model;
using NHibernate;
using System.Collections.Generic;
using ZLCMS.Business.Util;
using DotNet.Common.NHibernateUtil;
using NHibernate.Criterion;

namespace ZLCMS.Web.SystemManage.Weixin
{
    public partial class DeleteBigWheel : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            string ids = Request["ids"];
            if (!string.IsNullOrEmpty(ids))
            {
                string[] idStrs = ids.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                IList<int> idList = new List<int>();
                foreach (string acc in idStrs)
                {
                    if (!string.IsNullOrEmpty(acc))
                    {
                        idList.Add(Int32.Parse(acc));
                    }

                }

                //删除相应的素材记录
                DeleteMaterialById(idStrs);

                //根据ID,删除游戏配置记录
                using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    session.CreateSQLQuery("delete from Wx_Games_Config where Owner_Account = :account and Game_Type = 'bigwheel' and Recid in (:ids)").SetParameter("account", _userAccount)
                       .SetParameterList("ids", idList.ToArray())
                       .ExecuteUpdate();
                    session.Flush();
                    _framePage.Alert(new ReturnCode() { statusCode = 200, message = "操作成功!", navTabId = _framePage.NavTabId });
                }
            }
            else
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "参数错误" });
            }
        }

        #region 删除相应的素材记录 + protected void DeleteMaterialById(string[] IdArr)
        /// <summary>
        /// 删除相应的素材记录
        /// </summary>
        /// <param name="IdArr"></param>
        protected void DeleteMaterialById(string[] IdArr)
        {
            WxGamesConfig TempConfig;
            try
            { 
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                foreach (string acc in IdArr)
                {
                    if (!string.IsNullOrEmpty(acc))
                    {
                        //查询要删除的记录对象
                        TempConfig = session.CreateCriteria<WxGamesConfig>().Add(Expression.Eq("Id", int.Parse(acc))).UniqueResult<WxGamesConfig>();
                        //根据该对象的MaterialId删除相应的素材.
                        session.CreateSQLQuery("delete from Wx_Material where id = :mid ").SetParameter("mid", TempConfig.MaterialId).ExecuteUpdate();
                    }
                }
                session.Flush();
            }
            }
            catch(Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = ex.Message });
            }
            
        } 
        #endregion

    }
}